__author__ = 'yliu'

import os
import sys
from NetValueLoader import *
import NetValueBook
import util

def WriteHeader4(output_path,fund_table,prod):
	fd = open(output_path+"prod" + prod + ".nv_statistic.csv","w+")
	fd.write("#date,")
	flist = sorted(fund_table.items(), key=lambda d:d[1][2], reverse = False )
	for tuple in flist:
		name = tuple[0]
		fd.write("R%dC%d," % (fund_table[name][0][0],fund_table[name][0][1]))
	fd.write("\n")
	fd.write(",")
	for tuple in flist:
		name = tuple[0]
		fd.write("%s,"% name)
	fd.write("\n")
	return fd

def WriteHeader5(output_path,prod):
	fd = open(output_path + "\\"+ "prod" + prod + ".nv_info.csv","w+")
	fd.write("日期,股指期货期货保证金/净值(%),股票资产/净值(%),股指期货IF合约价值,股指期货IC合约价值,股指期货IH合约价值,\
	股票市值, 基金净值总额,基金单位净值,商品期货保证金占用,商品期货合约价值(不轧差计算),商品期货合约价值(轧差计算)\n")
	return fd


'''
note:create the nv_statisitc.csv file
date:20151203
'''
def runData4(sdate,edate,prod,disk,output_path):
	nv_path = disk + ":\\data\\WindDB\\production"+prod+"\\netValue\\"
	head_flag = True
	fd = None
	datelist = sorted(os.listdir(nv_path))
	for f in datelist:
		if len(f)!=19:
			continue
		date = f[0:8]
		if date < sdate or date > edate:
			continue
		nv = nvValue(date,prod,nv_path)
		ftable = nv.Load_FundTable()
		if head_flag is True:
			fd = WriteHeader4(output_path,ftable,prod)
			head_flag = False
		nv.WriteLine4(fd)
	fd.close()

def runData5(sdate,edate,prod,disk,output_path):
	nv_path = disk + ":\\data\\WindDB\\production"+prod+"\\netValue\\"
	datelist = sorted(os.listdir(nv_path))
	fd = WriteHeader5(output_path,prod)
	for f in datelist:
		if len(f)!=19:
			continue
		date = f[0:8]
		if date < sdate or date > edate:
			continue
		nv = nvValue(date,prod,nv_path)
		nv.Load_FundTable()
		nv.Load_FutureInfo()
		nv.Load_StockInfo()
		nv.WriteLine5(fd)
	fd.close()

def runFuturesData(sdate,edate,prod,disk,output_path):
	nv_path = "P:\\NetValue\\production"+prod+"\\netValue\\"
	datelist = sorted(os.listdir(nv_path))
	for f in datelist:
		if "prod" not in f or "xlsx" not in f:
			continue
		date = f[0:8]
		if sdate == edate:
			date = sdate
		elif date < sdate or date > edate:
			continue
		nv = nvValue(date,prod,nv_path)
		nv.Load_FutureInfo()
		nv.Load_FuutresTradeDetail()
		nv.OutputFuturesTrade()
		nv.OutputFuturesHolding()
		if sdate == edate:
			break

def runPNL(sdate,edate,prod,output_path):
	nv_path = "P:\\NetValue\\production"+prod+"\\netValue\\"
	datelist = sorted(os.listdir(nv_path))
	fd = open(output_path+"\\prod14.pnl.total.csv","w")
	fd.write("#date,cicc_pnl\n")
	for f in datelist:
		if "prod" not in f or "xlsx" not in f:
			continue
		date = f[0:8]
		if sdate == edate:
			date = sdate
		elif date < sdate or date > edate:
			continue
		nv = nvValue(date,prod,nv_path)
		nv.Load_FundTable()
		fd.write("%s,%.2f\n" % (date,nv.fund_table["总盈亏（扣除已提取损益）"][1]))
	fd.close()




if __name__ == '__main__':
	args= util.extract_opt(sys.argv)
	print(args)
	if args['type'] == "5":
		runData5(args['start'],args['end'],args['prod'],args['disk'],args['output'])
	elif args['type'] == "4":
		runData4(args['start'],args['end'],args['prod'],args['disk'],args['output'])
	elif args['type'] == "6":
		NetValueBook.runNetValueBook(args['start'],args['end'],args['prod'],args['disk'],args['output'])
	elif args['type'] == "7":
		runFuturesData(args['start'],args['end'],args['prod'],args['disk'],args['output'])
	elif args['type'] == "8":
		runPNL(args['start'],args['end'],args['prod'],args['output'])
	else:
		print("please input the right type number")
